using System;
using System.Threading.Tasks;
using JlPay.Sdk.Core;
using JlPay.Sdk.Services.Cashier;
using JlPay.Sdk.Models.Cashier;

namespace JlPay.Sdk.Examples.Cashier.TradeCancel
{
    /// <summary>
    /// 交易撤销独立示例程序
    /// </summary>
    public class TradeCancelDemo
    {
        /// <summary>
        /// 主程序入口
        /// </summary>
        /// <param name="args">命令行参数</param>
        /// <returns>任务</returns>
        public static async Task Main(string[] args)
        {
            Console.WriteLine("==========================================");
            Console.WriteLine("===== 交易撤销独立示例程序 =====");
            Console.WriteLine("==========================================");
            Console.WriteLine();

            await TradeCancelDemoAsync();

            Console.WriteLine("==========================================");
            Console.WriteLine("===== 示例运行完毕 =====");
            Console.WriteLine("==========================================");
        }

        /// <summary>
        /// 创建配置对象
        /// </summary>
        /// <returns>配置对象</returns>
        private static Config CreateConfig()
        {
            return DemoConfig.CreateConfigWithCrypto();
        }

        /// <summary>
        /// 构建交易撤销请求
        /// </summary>
        /// <returns>请求对象</returns>
        private static TradeCancelRequest BuildTradeCancelRequest()
        {
            var request = new TradeCancelRequest
            {
                MerchNo = "849584358120018",
                OutTradeNo = "ld201911023465530",
                OriOutTradeNo = "ld201911023465529",
                Remark = "撤销交易"
            };

            return request;
        }

        /// <summary>
        /// 交易撤销示例
        /// </summary>
        public static async Task TradeCancelDemoAsync()
        {
            Console.WriteLine("开始执行交易撤销示例");
            
            try
            {
                // 创建配置对象和收银台服务
                var config = CreateConfig();
                var cashierService = new CashierService(config);
                
                // 创建交易撤销请求
                var request = BuildTradeCancelRequest();
                
                // 发送请求
                var response = await cashierService.CancelTradeAsync(request);
                
                // 处理响应结果
                response.HandleResponse("交易撤销", config);
            }
            catch (Exception ex)
            {
                var config = CreateConfig();
                var response = new TradeCancelResponse();
                response.HandleResponseWithException("交易撤销", config, ex);
            }
            
            Console.WriteLine("交易撤销示例执行完毕");
        }
    }
} 